//
//  UIButton_Line.m
//  MedicalProject
//
//  Created by lifayu on 16/12/15.
//  Copyright © 2016年 贵阳飞云科技有限公司. All rights reserved.
//

#import "UIButton_Line.h"

@interface UIButton_Line()

@property(assign, nonatomic) CGFloat lineViewHeight;

@end

@implementation UIButton_Line
{
    NSInteger _lineViewType;//1:forButton 2:forTitle
    UIView *_lineView;
    UIControlState _lineViewState;

}

- (CGFloat)lineViewHeight
{
    if(_lineViewHeight < 0.01)
    {
        _lineViewHeight = 0.5;
    }
    return _lineViewHeight;
}

- (void)addLineViewColor:(UIColor *)color forTitleInState:(UIControlState)state lineHeight:(CGFloat)lineHeight
{
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = color;
    _lineView = lineView;
    _lineViewState = state;
    _lineViewHeight = lineHeight;
    _lineViewType = 2;
    [self addSubview:lineView];
    [self judgeLineViewState];
}

- (void)addLineViewColor:(UIColor *)color forButtonInState:(UIControlState)state lineHeight:(CGFloat)lineHeight
{
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = color;
    _lineView = lineView;
    _lineViewState = state;
    _lineViewType = 1;
    _lineViewHeight = lineHeight;
    [self addSubview:lineView];
    [self judgeLineViewState];
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    [self relayoutFrameOfSubViews];
}

- (void)relayoutFrameOfSubViews
{
    CGFloat width;
    CGFloat left;
    if(_lineViewType == 1)
    {
        left = 0;
        width = self.width;
    }
    else if (_lineViewType == 2)
    {
        left = self.titleLabel.left;
        width = self.titleLabel.width;
    }
    _lineView.left = left;
    _lineView.width = width;
    _lineView.height = _lineViewHeight;
    _lineView.top = self.height - _lineViewHeight;
}


- (void)setSelected:(BOOL)selected
{
    [super setSelected:selected];
    [self judgeLineViewState];
}

- (void)judgeLineViewState
{
    if(_lineViewState == UIControlStateSelected)
    {
        if(self.selected)
        {
            _lineView.hidden = NO;
        }
        else
        {
            _lineView.hidden = YES;
        }
    }
    else if(_lineViewState == UIControlStateNormal)
    {
        if(self.selected)
        {
            _lineView.hidden = YES;
        }
        else
        {
            _lineView.hidden = NO;
        }
    }
}


@end
